package com.dp.appi.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.*;
/**
 * Created by Administrator on 2016/9/12.
 */
public class FileHandel2 {
    public static String zip(String srcPathName, String zipFileName)
    {
        File file = new File(srcPathName);
        File zipFile = new File(zipFileName);
        if (!file.exists()) throw new RuntimeException(srcPathName + "不存在！");
        try
        {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
            ZipOutputStream out = new ZipOutputStream(cos);
            Properties pro=System.getProperties();
            String osName=pro.getProperty("os.name");
            if("Linux".equals(osName)||"linux".equals(osName)){
                out.setEncoding("GBK");//设置文件名编码方式
            }else{
                out.setEncoding(System.getProperty("sun.jnu.encoding"));//设置文件名编码方式
            }

            String basedir = "";
            compress(file, out, basedir);

            out.close();
            System.out.println(new Date() + "打包结束并关闭流");
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        System.out.println(new Date()+"项目文件打包成功并返回");
        String[] z=srcPathName.split("/");
        return z[z.length-1]+".zip";
    }
    /*
       * inputFileName 输入一个文件夹
       * zipFileName 输出一个压缩文件夹
       */
    private static void compress(File file, ZipOutputStream out, String basedir)
    {
    /* 判断是目录还是文件 */
        if (file.isDirectory())
        {
            // System.out.println("压缩：" + basedir + file.getName());
            compressDirectory(file, out, basedir);
        }
        else
        {
            // System.out.println("压缩：" + basedir + file.getName());
            compressFile(file, out, basedir);
        }
    }

    /** 压缩一个目录 */
    private static void compressDirectory(File dir, ZipOutputStream out, String basedir)
    {
        if (!dir.exists()) return;

        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++)
        {
      /* 递归 */
            compress(files[i], out, basedir + dir.getName() + "/");
        }
    }

    /** 压缩一个文件 */
    private static void compressFile(File file, ZipOutputStream out, String basedir)
    {
        if (!file.exists())
        {
            return;
        }
        try
        {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(basedir + file.getName());
            Properties pro=System.getProperties();
            String osName=pro.getProperty("os.name");
            if("Linux".equals(osName)||"linux".equals(osName)){
                entry.setUnixMode(644);
            }
            out.putNextEntry(entry);
            int count;
            byte data[] = new byte[8192];
            while ((count = bis.read(data, 0, 8192)) != -1)
            {
                out.write(data, 0, count);
            }
            bis.close();
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }


//    public static void main(String[] args) {
//        System.out.print(FileHandel2.zip("G:\\home", "G:\\home.zip"));
//    }
}
